package com.hhx.util;

import java.io.File;
import java.sql.*;

import org.apache.commons.configuration.XMLConfiguration;



/**
 * sqlserver数据库连接
 * @author vsked
 *
 */
public class ConnectDB {
    private static Connection conn = null;
    private static PreparedStatement pt = null;
    private static ResultSet rs = null;
    private static boolean flag = false;
    private static String sql = "";
    
    /**
     * 得到sqlserver数据库连接
     * @return Connection
     * @throws Exception
     */
	public static Connection getSqlServerConnection() throws Exception{
		XMLConfiguration config = new XMLConfiguration(((new File(ConnectDB.class.getResource("/").getPath()).getPath()).replace("\\", "/")+"/DBConfig.xml").replace("%20", " "));
		String type1=config.getString("type");
		String driver1=config.getString("driver");
		String address1=config.getString("address");
		String port1=config.getString("port");
		String database1=config.getString("database");
		String userName1=config.getString("username");
		String passWord1=config.getString("password");
		Class.forName(driver1);
		return DriverManager.getConnection("jdbc:microsoft:sqlserver://"+address1+":"+port1+";databasename="+database1, userName1, passWord1);
	}
	
	/**
	 * 关闭数据库连接
	 */
	public static void closeSqlConnection() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pt != null) {
				pt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	

	
	public static void main(String[] args) throws Exception {
		System.out.println(ConnectDB.getSqlServerConnection());
	}//end main

}
